package com.zhihao.domain.credit.service.adjust;

import com.zhihao.domain.credit.event.CreditExchangeRaffleCountSuccessMessageEvent;
import com.zhihao.domain.credit.model.aggregate.TradeAggregate;
import com.zhihao.domain.credit.model.entity.CreditAccountEntity;
import com.zhihao.domain.credit.model.entity.CreditOrderEntity;
import com.zhihao.domain.credit.model.entity.TaskEntity;
import com.zhihao.domain.credit.model.entity.TradeEntity;
import com.zhihao.domain.credit.model.valobj.TradeTypeVO;
import com.zhihao.domain.credit.repository.ICreditRepository;
import com.zhihao.domain.credit.service.ICreditAdjustService;
import com.zhihao.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Jackson
 * @description 积分调额服务【正逆向，增减积分】
 * @create 2024-06-01 09:36
 */
@Slf4j
@Service
public class CreditAdjustService implements ICreditAdjustService {

    @Resource
    private ICreditRepository creditRepository;
    @Resource
    private CreditExchangeRaffleCountSuccessMessageEvent creditAdjustSuccessMessageEvent;

    @Override
    public String createOrder(TradeEntity tradeEntity) {
        log.info("账户积分额度开始 userId:{} tradeName:{} amount:{}", tradeEntity.getUserId(), tradeEntity.getTradeName(), tradeEntity.getAmount());
        // 1. 创建账户积分实体
        CreditAccountEntity creditAccountEntity = TradeAggregate.createCreditAccountEntity(
                tradeEntity.getUserId(),
                tradeEntity.getAmount());

        // 2. 创建账户订单实体
        CreditOrderEntity creditOrderEntity = TradeAggregate.createCreditOrderEntity(
                tradeEntity.getUserId(),
                tradeEntity.getTradeName(),
                tradeEntity.getTradeType(),
                tradeEntity.getAmount(),
                tradeEntity.getOutBusinessNo());

        // 3. 构建消息任务对象(只有在”积分兑换抽奖次数“的时候才需要写task表+发送mq，暂时先不考虑后续扩展，先把目前业务做好)
        // 目前只有增加/兑换扣减操作：可以暂时根据”交易类型tradeType“，正向：task=null且不构建taskEntity-mq消息；反向：构建！
        TaskEntity taskEntity = null;
        String tradeType = tradeEntity.getTradeType().getCode();
        if ("reverse".equals(tradeType)){
            CreditExchangeRaffleCountSuccessMessageEvent.CreditExchangeRaffleCountSuccessMessage creditExchangeRaffleCountSuccessMessageMessage = new CreditExchangeRaffleCountSuccessMessageEvent.CreditExchangeRaffleCountSuccessMessage();
            creditExchangeRaffleCountSuccessMessageMessage.setUserId(tradeEntity.getUserId());
            creditExchangeRaffleCountSuccessMessageMessage.setOrderId(creditOrderEntity.getOrderId());
            creditExchangeRaffleCountSuccessMessageMessage.setAmount(tradeEntity.getAmount());
            creditExchangeRaffleCountSuccessMessageMessage.setOutBusinessNo(tradeEntity.getOutBusinessNo());
            BaseEvent.EventMessage<CreditExchangeRaffleCountSuccessMessageEvent.CreditExchangeRaffleCountSuccessMessage> creditAdjustSuccessMessageEventMessage = creditAdjustSuccessMessageEvent.buildEventMessage(creditExchangeRaffleCountSuccessMessageMessage);
            // mq消息
            taskEntity = TradeAggregate.createTaskEntity(tradeEntity.getUserId(), creditAdjustSuccessMessageEvent.topic(), creditAdjustSuccessMessageEventMessage.getId(), creditAdjustSuccessMessageEventMessage);
            log.info("reverse积分反向操作，credit_exchange_raffle_count_success消息队列mq+task已构建！");
        }
        // 4. 构建交易聚合对象
        TradeAggregate tradeAggregate = TradeAggregate.builder()
                .userId(tradeEntity.getUserId())
                .creditAccountEntity(creditAccountEntity)
                .creditOrderEntity(creditOrderEntity)
                .taskEntity(taskEntity)
                .build();

        // 5. 保存积分交易订单
        creditRepository.saveUserCreditTradeOrder(tradeAggregate);
        log.info("账户积分额度完成 userId:{} orderId:{}", tradeEntity.getUserId(), creditOrderEntity.getOrderId());

        return creditOrderEntity.getOrderId();
    }

    @Override
    public CreditAccountEntity queryUserCreditAccount(String userId) {
        return creditRepository.queryUserCreditAccount(userId);
    }

}
